﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.LogicComponents {
    import Engine.Managers.*;
    import Classes.*;
    import Classes.util.*;
    import Display.Toaster.*;
    import Classes.gates.*;
    import Modules.stats.types.*;

    public class MunicipalComponentUpgradable extends MunicipalComponentBase {

        public function MunicipalComponentUpgradable(_arg1:Municipal){
            super(_arg1);
            m_gateFactory.register(GateType.INVENTORY, UpgradeInventoryGate);
            m_gateFactory.register(GateType.CREW, UpgradeCrewGate);
            this.m_gate = m_gateFactory.loadGateFromXML(m_municipal.getItem(), m_municipal, "pre_upgrade", this.onUpgradeSuccess);
            m_inExperiment = true;
            if (((m_municipal.getItem().upgrade) && (m_municipal.getItem().upgrade.experiment))){
                m_inExperiment = (Global.experimentManager.getVariant(m_municipal.getItem().upgrade.experiment) == 1);
            };
        }
        override public function passesExperimentGate():Boolean{
            return (((super.passesExperimentGate()) || ((m_municipal.getItem().level > 1))));
        }
        override protected function onUpgradeSuccess():void{
            var _local1:Array;
            var _local2:String;
            var _local3:ItemToaster;
            var _local4:String;
            super.onUpgradeSuccess();
            StatsManager.sample(100, StatsCounterType.DIALOG, StatsKingdomType.ACTIVE_BUILDING, ("unlock_" + m_municipal.getItem().name), m_municipal.getItem().upgrade.upgradeTrackingClass);
            this.m_municipal.setFullGrown();
            if (this.m_municipal.harvest()){
                this.m_municipal.doHarvestDropOff(true);
            };
            if (m_municipal.getItem().level >= 2){
                _local1 = Global.world.getObjectsByPredicate(isEqualOrHigherUpgradedMunicipal);
                _local2 = null;
                _local3 = null;
                if ((((_local1.length == 1)) && ((m_municipal.getItem().level == 2)))){
                    _local2 = ZLoc.t("Main", m_municipal.getItem().upgrade.firstTimeToasterText);
                } else {
                    if ((((_local1.length == 1)) && ((m_municipal.getItem().level > 2)))){
                        _local2 = ZLoc.t("Main", "UnlockToasterText", {itemName:this.m_municipal.getItem().localizedName});
                    };
                };
                _local4 = TOASTER_SAM;
                if (m_municipal.getItem().upgrade.toasterIcon){
                    _local4 = m_municipal.getItem().upgrade.toasterIcon;
                };
                if (((!((_local2 == null))) && ((_local2.length > 0)))){
                    _local3 = new ItemToaster("", _local2, Global.getAssetURL(_local4));
                    Global.ui.toaster.show(_local3, false);
                };
            };
        }
        override public function onPopulationInit(_arg1:int, _arg2:int, _arg3:int):void{
            m_lastPopulation = _arg1;
        }
        override public function onPopulationChanged(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
            var _local5:int = (_arg1 - m_lastPopulation);
            if (_local5 > 0){
                this.attemptUpgradeIntro();
            };
            m_lastPopulation = _arg1;
        }
        override public function onLevelChanged(_arg1:int):void{
            this.attemptUpgradeIntro();
        }
        override public function onBuildingConstructionCompleted():void{
            this.attemptUpgradeIntro();
        }
        protected function attemptUpgradeIntro():void{
            if (((((!(Global.player.getSeenFlag(m_municipal.getItem().upgrade.introSeenFlag))) && (m_inExperiment))) && (!(Global.isVisiting())))){
                if (this.m_municipal.isUpgradePossible()){
                    Global.player.setSeenFlag(m_municipal.getItem().upgrade.introSeenFlag);
                    this.showUpgradeIntro();
                };
            };
        }
        protected function showUpgradeIntro():void{
            if (Global.world.dooberManager.getDooberCount() > 0){
                TimerUtil.callLater(this.showUpgradeIntro, UPGRADE_INTRO_DELAY);
            } else {
                if (!m_cancelTutorial){
                    Global.guide.notify(m_municipal.getItem().upgrade.introGuideNotify);
                };
            };
        }

    }
}//package Classes.LogicComponents 
